package com.hanbang.taitaigou.ui.activity.aboutme;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hanbang.taitaigou.R;
import com.hanbang.taitaigou.ui.activity.aboutme.DistributionOfHeroes.AboutMeDistributionOfHeroesActivity;
import com.hanbang.taitaigou.ui.activity.aboutme.G20.AboutG20ShoppingCardActivity;
import com.hanbang.taitaigou.ui.activity.aboutme.address.AboutMeAddressManagemenntActivity;
import com.hanbang.taitaigou.ui.activity.aboutme.ambassador.AboutMePromotionAmbassadorActivity;
import com.hanbang.taitaigou.ui.activity.aboutme.coupon.AboutCouponActivity;
import com.hanbang.taitaigou.ui.activity.aboutme.myOrder.AboutMyOrderActivity;
import com.hanbang.taitaigou.ui.activity.aboutme.setting.AboutMeSetUpActivity;
import com.hanbang.taitaigou.ui.activity.application.MyApplication;
import com.hanbang.taitaigou.ui.activity.base.MainBaseActivity;
import com.hanbang.taitaigou.ui.widget.dialog.BaseDialog;
import com.hanbang.taitaigou.ui.widget.dialog.OnClickCallback;
import com.hanbang.taitaigou.utils.http.BitmapUtil;
import com.hanbang.taitaigou.utils.http.HttpCallBack;
import com.hanbang.taitaigou.utils.http.HttpLocalUtils;
import com.hanbang.taitaigou.utils.http.HttpRequestParams;
import com.hanbang.taitaigou.utils.ui.LogUtils;
import com.hanbang.taitaigou.utils.ui.SystemTools;
import com.hanbang.taitaigou.utils.ui.ToastUtils;

import java.io.File;
import java.io.IOException;


public class AboutMeActivity extends MainBaseActivity {

    /**
     * 设置
     **/
    private LinearLayout ll_about_set_up;
    private ImageView iv_set_up_2;
    /**
     * 成为推广大使
     **/
    private LinearLayout ll_about_promotion_ambassador;
    /**
     * 配送大侠
     **/
    private LinearLayout ll_about_distribution_of_heroes;
    /**
     * 地址管理
     **/
    private LinearLayout ll_about_address_management;
    /**
     * 头像
     **/
    private ImageView iv_about_me_head_portrait;
    /**
     * 充值
     **/
    private RelativeLayout rl_about_recharge;
    /**
     * 提现
     **/
    private RelativeLayout rl_rl_about_withdrawals;
    /**
     * G20购物卡
     **/
    private LinearLayout ll_about_g20_shopping_card;
    /**
     * 优惠券
     **/
    private LinearLayout ll_about_coupon;
    /**
     * 我的订单
     **/
    private LinearLayout ll_about_my_order;
    /**
     * 照片地址
     **/
    private String path;

    /**
     * 电话号码
     */
    private TextView tv_tel;


    private ImageView my;
    private TextView tv_my;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aboutme);
        initView();
        setOnClickListener();

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("LoginSuccess");
        intentFilter.addAction("tuichu");
        registerReceiver(broadcastReceiver, intentFilter);

    }


    /*
     * 设置点击事件
     */
    private void setOnClickListener() {
        ll_about_set_up.setOnClickListener(onClickListener);
        iv_set_up_2.setOnClickListener(onClickListener);
        ll_about_promotion_ambassador.setOnClickListener(onClickListener);
        ll_about_distribution_of_heroes.setOnClickListener(onClickListener);
        ll_about_address_management.setOnClickListener(onClickListener);
        iv_about_me_head_portrait.setOnClickListener(onClickListener);
        rl_about_recharge.setOnClickListener(onClickListener);
        rl_rl_about_withdrawals.setOnClickListener(onClickListener);
        ll_about_g20_shopping_card.setOnClickListener(onClickListener);
        ll_about_coupon.setOnClickListener(onClickListener);
        ll_about_my_order.setOnClickListener(onClickListener);
    }

    @SuppressWarnings("unused")
    private OnClickListener onClickListener = new OnClickListener() {
        Intent intent = new Intent();

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                //设置
                case R.id.ll_about_set_up:
                    intent.setClass(AboutMeActivity.this, AboutMeSetUpActivity.class);
                    startActivity(intent);
                    break;
                //设置2
                case R.id.iv_set_up_2:
                    intent.setClass(AboutMeActivity.this, AboutMeSetUpActivity.class);
                    startActivity(intent);
                    break;
                //推广大使
                case R.id.ll_about_promotion_ambassador:
                    if (isLogin(true)) {
                        intent.setClass(AboutMeActivity.this, AboutMePromotionAmbassadorActivity.class);
                        startActivity(intent);
                    }
                    break;
                //送货大侠
                case R.id.ll_about_distribution_of_heroes:
                    if (isLogin(true)) {
                        intent.setClass(AboutMeActivity.this, AboutMeDistributionOfHeroesActivity.class);
                        startActivity(intent);
                    }
                    break;
                //地址管理
                case R.id.ll_about_address_management:
                    if (!isLogin(true)) {

                    } else {
                        intent.setClass(AboutMeActivity.this, AboutMeAddressManagemenntActivity.class);
                        intent.putExtra("addressType", "2");
                        startActivity(intent);
                        
                    }
                    break;
                //头像照片
                case R.id.iv_about_me_head_portrait:
//                    //判断是否登入
                    if (!isLogin(true)) {

                    }
//                    imageManager.bind(iv_about_me_head_portrait, HttpLocalUtils.getHttpFileUrl(userData.getFace()));
                    BaseDialog dialog = new BaseDialog(AboutMeActivity.this, R.style.about_me_dialog, BaseDialog.MODE.HEAD);
                    dialog.setOnClickCallback(new OnClickCallback() {
                        @Override
                        public void onClick(BaseDialog dialog, Object number, Object content) {
                            //拍照
                            if ((Integer) number == 1) {
                                File file = new File(MyApplication.appSDCachePath);
                                if (file.exists() || file.mkdirs()) {
                                    path = System.currentTimeMillis() + ".jpg";

                                    File pathFile = new File(file, path);
                                    if (!pathFile.exists()) {
                                        try {
                                            pathFile.createNewFile();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(pathFile));
                                    startActivityForResult(intent, 998);
                                }
                                //相册选择
                            } else if ((Integer) number == 2) {
                                //获取相册
                                Intent intent = new Intent();
                                intent.setType("image/*");
                                intent.setAction(Intent.ACTION_GET_CONTENT);
                                startActivityForResult(intent, 999);
                            }
                        }
                    });
                    dialog.show();


                    break;
                //充值
                case R.id.rl_about_recharge:
                    intent.setClass(AboutMeActivity.this, AboutmeRechargeActivity.class);
                    startActivity(intent);
                    break;
                //提现
                case R.id.rl_rl_about_withdrawals:
                    intent.setClass(AboutMeActivity.this, AboutmeRechargeActivity.class);
                    startActivity(intent);
                    break;
                //G20购物卡
                case R.id.ll_about_g20_shopping_card:
                    intent.setClass(AboutMeActivity.this, AboutG20ShoppingCardActivity.class);
                    startActivity(intent);
                    break;
                //优惠券
                case R.id.ll_about_coupon:
                    if (!isLogin(true)) {
                    } else {
                        intent.setClass(AboutMeActivity.this, AboutCouponActivity.class);
                        startActivity(intent);
                    }
                    break;
                //我的订单
                case R.id.ll_about_my_order:
                    if (isLogin(true)) {
                        intent.setClass(AboutMeActivity.this, AboutMyOrderActivity.class);
                        startActivity(intent);
                    }
                    break;
                default:
                    break;
            }
        }
    };

    /**
     * intent 回调方法
     */

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /**
         * 用户更改头像
         */
        HttpRequestParams httpRequestParams = new HttpRequestParams("UserUpdateHead");
        if (requestCode == 999 && resultCode == -1) {
            Uri uri = data.getData();
            path = SystemTools.getPath(this, uri);
            LogUtils.e("path", path.toString());
//            ss = path.substring(44);
            httpRequestParams.addUserId(userData);
            httpRequestParams.addBodyParameter("avatar", new File(path));
            httpManager.post(httpRequestParams, new HttpCallBack<String>(this, "正在上传...") {
                @Override
                public void onSuccess(String result) {
                    super.onSuccess(result);
                    imageManager.bind(iv_about_me_head_portrait, path, BitmapUtil.getImageOptionsBuilder().setCircular(true).build());
                }
            });


        } else if (requestCode == 998 && resultCode == -1) {
            File fDir = new File(MyApplication.appSDCachePath);
            LogUtils.e("wwwwwwwwwwwwwww", fDir.getPath());
            if (fDir.exists() || fDir.mkdirs()) {
                File cameraFile = new File(fDir, path);
                path = System.currentTimeMillis() + ".jpg";
                LogUtils.e("path", path.toString());
                if (cameraFile.exists()) {
                    path = cameraFile.getPath();
                    httpRequestParams.addUserId(userData);
                    httpRequestParams.addBodyParameter("avatar", new File(path));
                    httpManager.post(httpRequestParams, new HttpCallBack<String>(this, "正在上传...") {
                        @Override
                        public void onSuccess(String result) {
                            super.onSuccess(result);
                            imageManager.bind(iv_about_me_head_portrait, path, BitmapUtil.getImageOptionsBuilder().setCircular(true).build());
                        }
                    });
                } else {
                    ToastUtils.show(this, "获取照片失败！", Toast.LENGTH_SHORT);
                }
            } else {
                ToastUtils.show(this, "获取照片失败！", Toast.LENGTH_SHORT);
            }
        }
    }

    /*
     * 初始化view
     */
    private void initView() {
        tv_my = (TextView)findViewById(R.id.tv_my);
        tv_my.setTextColor(getResources().getColor(R.color.black));
        my = (ImageView)findViewById(R.id.my);
        my.setImageResource(R.mipmap.my_1);



        // 设置顶部的标题
        setTop(null, 0, getString(R.string.user_zx), null, 0);
        setCurrentBotton(3);
        ll_about_set_up = (LinearLayout) findViewById(R.id.ll_about_set_up);
        iv_set_up_2 = (ImageView)findViewById(R.id.iv_set_up_2);
        ll_about_promotion_ambassador = (LinearLayout) findViewById(R.id.ll_about_promotion_ambassador);
        ll_about_distribution_of_heroes = (LinearLayout) findViewById(R.id.ll_about_distribution_of_heroes);
        ll_about_address_management = (LinearLayout) findViewById(R.id.ll_about_address_management);
        iv_about_me_head_portrait = (ImageView) findViewById(R.id.iv_about_me_head_portrait);
        rl_about_recharge = (RelativeLayout) findViewById(R.id.rl_about_recharge);
        rl_rl_about_withdrawals = (RelativeLayout) findViewById(R.id.rl_rl_about_withdrawals);
        ll_about_g20_shopping_card = (LinearLayout) findViewById(R.id.ll_about_g20_shopping_card);
        ll_about_coupon = (LinearLayout) findViewById(R.id.ll_about_coupon);
        ll_about_my_order = (LinearLayout) findViewById(R.id.ll_about_my_order);
        tv_tel = (TextView) findViewById(R.id.tv_tel);


        if (isLogin(false)) {
            imageManager.bind(iv_about_me_head_portrait, HttpLocalUtils.getHttpFileUrl(userData.getFace()), BitmapUtil.getImageOptionsBuilder().setCircular(true).build());
            tv_tel.setText(userData.getMobile().toString());
        }
    }


    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().toString().equals("LoginSuccess")) {
                imageManager.bind(iv_about_me_head_portrait, HttpLocalUtils.getHttpFileUrl(userData.getFace()), BitmapUtil.getImageOptionsBuilder().setCircular(true).build());
                tv_tel.setText(userData.getMobile());
                LogUtils.e("aaa", "收到");
            }else if(intent.getAction().toString().equals("tuichu")){
                iv_about_me_head_portrait.setImageResource(R.mipmap.head_portrait);
                tv_tel.setText("登入");
            }
        }
    };


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(broadcastReceiver);
    }
}
